from django.db import models
from django.contrib.auth.models import User
from meetings.models import Group


class DailyReport(models.Model):
    """日报"""
    STATUS_CHOICES = [
        ('pending', '待审核'),
        ('approved', '已通过'),
        ('rejected', '已驳回'),
    ]
    
    group = models.ForeignKey(Group, on_delete=models.CASCADE, related_name='reports', verbose_name='所属小组', db_comment='所属小组ID')
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='daily_reports', verbose_name='提交者', db_comment='提交者ID')
    report_date = models.DateField(verbose_name='日报日期', db_comment='日报日期')
    content = models.TextField(verbose_name='日报内容', db_comment='日报内容')
    work_summary = models.TextField(verbose_name='工作总结', db_comment='工作总结')
    next_plan = models.TextField(null=True, blank=True, verbose_name='明日计划', db_comment='明日计划')
    issues = models.TextField(null=True, blank=True, verbose_name='遇到的问题', db_comment='遇到的问题')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='pending', verbose_name='状态', db_comment='状态:pending待审核,approved已通过,rejected已驳回')
    submitted_at = models.DateTimeField(auto_now_add=True, verbose_name='提交时间', db_comment='提交时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间', db_comment='更新时间')

    class Meta:
        verbose_name = '日报'
        verbose_name_plural = '日报'
        db_table = 'zhahuo_daily_report'
        db_table_comment = '日报信息表'
        unique_together = ['group', 'user', 'report_date']
        ordering = ['-report_date']

    def __str__(self):
        return f'{self.user.username} - {self.report_date}'


class ReportReview(models.Model):
    """日报审批"""
    STATUS_CHOICES = [
        ('approved', '已通过'),
        ('rejected', '已驳回'),
    ]
    
    report = models.ForeignKey(DailyReport, on_delete=models.CASCADE, related_name='reviews', verbose_name='日报', db_comment='日报ID')
    reviewer = models.ForeignKey(User, on_delete=models.CASCADE, related_name='report_reviews', verbose_name='审批人', db_comment='审批人ID')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, verbose_name='状态', db_comment='状态:approved已通过,rejected已驳回')
    comment = models.TextField(null=True, blank=True, verbose_name='批注', db_comment='批注')
    reviewed_at = models.DateTimeField(auto_now_add=True, verbose_name='审批时间', db_comment='审批时间')

    class Meta:
        verbose_name = '日报审批'
        verbose_name_plural = '日报审批'
        db_table = 'zhahuo_report_review'
        db_table_comment = '日报审批表'

    def __str__(self):
        return f'{self.reviewer.username} 审批 {self.report.user.username} 的日报'
